package com.ddwl.message.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.MessageEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.message.service.NoticeService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.message.NoticeBo;
import com.ddwl.schema.bo.message.NoticeSearchBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.message.NoticeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;


@Api(tags = "消息服务")
@RequestMapping("notice")
@Auth(product = BaseEnum.ProductEnum.CONSOLE)
@RestController
public class NoticeController extends BaseController {

    @Autowired
    private NoticeService noticeService;

    @ApiOperation("首页快报")
    @Auth(product = {BaseEnum.ProductEnum.ZHENYU, BaseEnum.ProductEnum.CLOTHES, BaseEnum.ProductEnum.MALL})
    @PostMapping("/index")
    public RestVo<List<NoticeVo>> index(@Valid @RequestBody PageBo<NoticeSearchBo> pageBo) {
        NoticeSearchBo param = Optional.ofNullable(pageBo.getParam()).orElse(new NoticeSearchBo());
        param.setProduct(getProduct());
        param.setStatus(MessageEnum.NoticeStatus.VALID.getCode());
        param.setType(MessageEnum.NoticeType.ACTIVITY.getCode());
        return RestVo.ok(noticeService.index(pageBo));
    }

    @PostMapping("page")
    @ApiOperation("分页查询通知")
    public RestVo<PageVo<NoticeVo>> page(@Valid @RequestBody PageBo<NoticeSearchBo> pageBo) {
        return RestVo.ok(noticeService.page(pageBo));
    }

    @PostMapping("/create")
    @ApiOperation("创建通知")
    public RestVo<String> create(@Valid @RequestBody NoticeBo bo) {
        return RestVo.ok(noticeService.create(bo));
    }

    @PostMapping("/update")
    @ApiOperation("修改通知")
    public RestVo update(@Valid @RequestBody NoticeBo bo) {
        noticeService.update(bo);
        return RestVo.ok();
    }

    @DeleteMapping("del")
    @ApiOperation("删除通知")
    public RestVo delete(@RequestParam String id) {
        noticeService.delete(id);
        return RestVo.ok();
    }

    @GetMapping("/detail")
    @Auth(product = {BaseEnum.ProductEnum.ZHENYU, BaseEnum.ProductEnum.CLOTHES, BaseEnum.ProductEnum.MALL})
    @ApiOperation("通知详情")
    public RestVo<NoticeVo> detail(@ApiParam(value = "通知ID", required = true) @RequestParam String id) {
        return RestVo.ok(noticeService.detail(id));
    }


}
